package com.notebad.ui;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.notebad.GeneralVariables;
import com.notebad.R;
import com.notebad.note.Note;
import com.notebad.ui.preferences.PreferencesView;

/**
 * @author Alex
 *
 *	Main apps Activity. This activity is loading first (see AndroidManifest.xml file). 
 */
public class WelcomeView extends Activity {

	private static final String TAG = "WelcomeView";
	private static final int PREF_ID = Menu.FIRST;

	private ImageButton editStartButton;
	private ImageButton listStartButton;
	private ImageButton imgStartButton;
	private ImageButton recordStartButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		editStartButton = (ImageButton) findViewById(R.id.StartEdit);
		listStartButton = (ImageButton) findViewById(R.id.StartList);
		imgStartButton = (ImageButton) findViewById(R.id.StartImg);
		recordStartButton = (ImageButton) findViewById(R.id.StartRecord);

		//Create application directory if it does not exist
		File notebadFolder = new File(GeneralVariables.APLLICATION_FOLDER);
		if(!notebadFolder.exists() || !notebadFolder.isDirectory()){
			notebadFolder.mkdir();
		}

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				switch(view.getId()) {
				case R.id.StartEdit:						
					Log.d(TAG, "Start Edit");
					Note.initCurrentNote();
					Intent textView = new Intent(WelcomeView.this, VisualisationView.class);
					textView.putExtra("tab", 0);
					startActivity(textView);
					return;
				case R.id.StartRecord:
					Log.d(TAG, "Start Record");
					Note.initCurrentNote();
					Intent audio = new Intent(WelcomeView.this, AudioRecorderView.class);
					startActivity(audio);
					return;
				case R.id.StartImg:
					Log.d(TAG, "Start Picture");
					Note.initCurrentNote();
					Intent cam = new Intent(WelcomeView.this, CameraView.class);
					startActivity(cam);
					return;
				case R.id.StartList:
					Log.d(TAG, "Start List");
					Intent list = new Intent(WelcomeView.this, NoteListView.class);
					startActivity(list);
					return;
				}
			}
		};

		editStartButton.setOnClickListener(listener);
		listStartButton.setOnClickListener(listener);
		imgStartButton.setOnClickListener(listener);
		recordStartButton.setOnClickListener(listener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, PREF_ID, 0, R.string.menu_preferences).setIcon(R.drawable.ic_menu_preferences);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case PREF_ID:
			Log.d(TAG, "Menu - select Preferences");
			startActivity(new Intent(this, PreferencesView.class));
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}